/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Programa;
import static co.edu.senaclem.persistencia.Persistencia.areadb;
import static co.edu.senaclem.persistencia.Persistencia.con;
import static co.edu.senaclem.persistencia.Persistencia.conectar;
import static co.edu.senaclem.persistencia.Persistencia.desconectar;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author anfeles
 */
@Stateless
public class ProgramaDAO implements IProgramaDAO {
    public static ResultSet resSet=null;
    
    @Override
    public void eliminar(Programa programa) throws Exception {
        try {
            if (conectar()) {
                String consulta = "delete from programa where codigoprograma='" +programa.getCodigo()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Programa programa) throws Exception {
        try {
            if (conectar()) {
                String consulta = "update programa set nombreprograma='" + programa.getNombre()+ "' where codigoprograma='"+programa.getCodigo()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void insertar(Programa programa) throws Exception {
        try {
            if (conectar()) {
                String consulta = "insert into programa values('" + programa.getCodigo() + "','" + programa.getNombre() + "')";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Programa consultarPorId(int codigo) throws Exception {
        try {
            Programa miPrograma = null;
            if (conectar()) {
                String consulta = "Select * From programa where codigoprograma='" + codigo + "'";
                areadb = con.createStatement();
                resSet = areadb.executeQuery(consulta);

                if (resSet.next()) {
                    miPrograma = new Programa();
                    miPrograma.setCodigo(resSet.getInt("codigoprograma"));
                    miPrograma.setNombre(resSet.getString("nombreprograma"));                    
                }

                resSet.close();
                desconectar();
            }

            return miPrograma;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Programa> consultar() throws Exception {
        try {
            List<Programa> programas = new ArrayList<Programa>();
            if (conectar()) {
                String consulta = "Select * From programa";
                areadb = con.createStatement();
                resSet = areadb.executeQuery(consulta);

                while (resSet.next()) {
                    Programa miPrograma = new Programa();
                    miPrograma.setCodigo(resSet.getInt("codigoprograma"));
                    miPrograma.setNombre(resSet.getString("nombreprograma"));

                    programas.add(miPrograma);
                }

                resSet.close();
                desconectar();
            }

            return programas;
        } catch (RuntimeException e) {
            throw e;
        }
    }

}
